package com.branches.imgs;

import javax.imageio.ImageIO;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 * @author wujian
 * @date 2024/5/22
 */
public class CombineImageUtil {

    public static List<CombineImage> combineImages(List<OCRExamItem> itemList) {
        itemList.sort(OCRExamItem::sort);

        List<CombineImage> combineImages = new ArrayList<>();
        CombineImage combineImage = new CombineImage();
        combineImages.add(combineImage);
        for (OCRExamItem item : itemList) {
            boolean v = combineImage.addItemOnX(item);
            if (!v) {
                combineImage = new CombineImage();
                combineImages.add(combineImage);
                combineImage.addItemOnX(item);
            }
        }

        int currentIndex = 0;
        for (int i = 1; i < combineImages.size(); i++) {
            combineImage = combineImages.get(currentIndex);
            if(!combineImage.addItemOnY(combineImages.get(i))) {
                currentIndex = i;
                i++;
            }
        }

        List<CombineImage> images = combineImages.stream().filter(CombineImage::isValid).collect(Collectors.toList());
        String rootDir = "/home/branches/java/boot/merges/";
        images.parallelStream().forEach(image -> {
            try {
                ImageIO.write(image.getFile(), "jpg", new File(rootDir + String.valueOf(image.getId()) + ".jpg"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        return images;
    }


    public static String readFileByBytes() {
        String fileName = "/home/branches/java/boot/merges/0.json";

        // 读取文件内容到Stream流中，按行读取
        Stream<String> lines = null;
        StringBuffer buffer = new StringBuffer("");
        try {
            lines = Files.lines(Paths.get(fileName));
            // 随机行顺序进行数据处理
            lines.forEach(ele -> {
                buffer.append(ele);
            });
        } catch (IOException e) {
            e.printStackTrace();
        }

        return buffer.toString();

    }

    public static void main(String[] args) {
        long time = System.currentTimeMillis();
        File root = new File("/home/branches/java/boot/dls/");
        File[] files = root.listFiles();
        long id = 1;
        List<OCRExamItem> itemList = new ArrayList<>();
        for (File file : files) {
            OCRExamItem item = new OCRExamItem();
            item.setItemId(id++);
            item.setFile(file);
            itemList.add(item);
            item.init();
        }
        // 合并图片
        List<CombineImage> images = combineImages(itemList);
        images.forEach(image -> {
            String data = readFileByBytes();
            RecogParse recogParse = new RecogParse(image, data);
            recogParse.parse();
        });
    }


}
